package com.hs.hlw.credit.system.business.service.impl;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.dto.SubmissionFormFieldDto;
import com.hs.hlw.credit.system.business.dto.UserDto;
import com.hs.hlw.credit.system.business.service.SubmissionFormFieldService;
import com.hs.hlw.credit.system.common.enumeration.*;
import com.hs.hlw.credit.system.common.util.JsonUtil;
import com.hs.hlw.credit.system.data.primary.mapper.DataSourceMapper;
import com.hs.hlw.credit.system.data.primary.mapper.SubmissionFormFieldMapper;
import com.hs.hlw.credit.system.data.primary.mapper.SubmissionFormMapper;
import com.hs.hlw.credit.system.data.primary.model.DataSource;
import com.hs.hlw.credit.system.data.primary.model.SubmissionForm;
import com.hs.hlw.credit.system.data.primary.model.SubmissionFormField;
import com.hs.hlw.credit.system.data.primary.model.SubmissionFormFieldExample;
import com.hualongdata.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * SubmissionFormFieldService
 * @author zhb
 * @date 2018/11/21
 */
@Service
public class SubmissionFormFieldServiceImpl implements SubmissionFormFieldService {
    private @Autowired SubmissionFormFieldMapper submissionFormFieldMapper;
    private @Autowired SubmissionFormMapper submissionFormMapper;
    private @Autowired DataSourceMapper dataSourceMapper;
    private static final short SQL = 2;

    /**
     * 更新文本输入规则
     * @param data
     * @param user
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto update1(SubmissionFormFieldDto.Update1 data, UserDto user) {
        SubmissionFormField field = submissionFormFieldMapper.selectByPrimaryKey(data.getId());
        if (field == null) {
            throw new BusinessException("字段不存在");
        }
        if (!FormFieldInputType.TYPE_1.code.equals(field.getInputType())) {
            throw new BusinessException("表单类型与更新不匹配");
        }

        // 更新字段规则配置
        data.setId(null);
        field.setRuleCfg(JsonUtil.toJson(data));
        submissionFormFieldMapper.updateByPrimaryKeySelective(field);

        // 更新表单
        updateForm(field, user);

        return new ResponseDto("操作成功");
    }

    /**
     * 更新数值输入规则
     * @param data
     * @param user
     * @return
     */
    public ResponseDto update2(SubmissionFormFieldDto.Update2 data, UserDto user) {
        SubmissionFormField field = submissionFormFieldMapper.selectByPrimaryKey(data.getId());
        if (field == null) {
            throw new BusinessException("字段不存在");
        }
        if (!FormFieldInputType.TYPE_2.code.equals(field.getInputType())) {
            throw new BusinessException("表单类型与更新不匹配");
        }

        // 更新字段规则配置
        data.setId(null);
        field.setRuleCfg(JsonUtil.toJson(data));
        submissionFormFieldMapper.updateByPrimaryKeySelective(field);

        // 更新表单
        updateForm(field, user);

        return new ResponseDto("操作成功");
    }

    /**
     * 更新维表选择规则
     * @param data
     * @param user
     * @return
     */
    public ResponseDto update3(SubmissionFormFieldDto.Update3 data, UserDto user) {
        SubmissionFormField field = submissionFormFieldMapper.selectByPrimaryKey(data.getId());
        if (field == null) {
            throw new BusinessException("字段不存在");
        }
        if (!FormFieldInputType.TYPE_3.code.equals(field.getInputType())) {
            throw new BusinessException("表单类型与更新不匹配");
        }
        if (data.getDataSourceType() == SQL) {
            if (data.getDataSourceId() == null) {
                throw new BusinessException("未选择数据源");
            }
            if (dataSourceMapper.selectByPrimaryKey(data.getDataSourceId()) == null) {
                throw new BusinessException("数据源不存在");
            }
        }

        // 更新字段规则配置
        data.setId(null);
        field.setRuleCfg(JsonUtil.toJson(data));
        submissionFormFieldMapper.updateByPrimaryKeySelective(field);

        // 更新表单
        updateForm(field, user);

        return new ResponseDto("操作成功");
    }

    /**
     * 更新日期选择规则
     * @param data
     * @param user
     * @return
     */
    public ResponseDto update4(SubmissionFormFieldDto.Update4 data, UserDto user) {
        SubmissionFormField field = submissionFormFieldMapper.selectByPrimaryKey(data.getId());
        if (field == null) {
            throw new BusinessException("字段不存在");
        }
        if (!FormFieldInputType.TYPE_4.code.equals(field.getInputType())) {
            throw new BusinessException("表单类型与更新不匹配");
        }

        // 更新字段规则配置
        data.setId(null);
        field.setRuleCfg(JsonUtil.toJson(data));
        submissionFormFieldMapper.updateByPrimaryKeySelective(field);

        // 更新表单
        updateForm(field, user);

        return new ResponseDto("操作成功");
    }

    /**
     * 更新表单
     * @param field
     * @param user
     */
    private void updateForm(SubmissionFormField field, UserDto user) {
        if (field == null || field.getFormId() == null) {
            return;
        }
        if (user == null && user.getId() == null) {
            return;
        }
        SubmissionForm submissionForm = submissionFormMapper.selectByPrimaryKey(field.getFormId());
        if (submissionForm == null) {
            return;
        }
        submissionForm.setUpdateTime(new Date());
        submissionForm.setModifierId(user.getId());
        submissionForm.setModifierName(user.getUserName());
        submissionFormMapper.updateByPrimaryKeySelective(submissionForm);
    }

    /**
     * 根据表单获取字段列表
     * @param formId
     * @return
     */
    public ResponseDto getFiledListByForm(Integer formId) {
        SubmissionFormFieldExample example = new SubmissionFormFieldExample();
        SubmissionFormFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFormIdEqualTo(formId);
        List<SubmissionFormField> list = submissionFormFieldMapper.selectByExample(example);

        if (list != null && !list.isEmpty()) {
            SubmissionForm form = submissionFormMapper.selectByPrimaryKey(formId);
            DataSource dataSource = null;
            DataBaseType dataBaseType = null;
            if (form != null) {
                dataSource = dataSourceMapper.selectByPrimaryKey(form.getDataSourceId());
            }
            if (dataSource != null) {
                dataBaseType = DataBaseType.getDataBaseTypeByCode(dataSource.getDatabaseType());
            }
            for (SubmissionFormField field : list) {
                if (CommonStatus.STATUS_1.code.equals(field.getIsSearchCondition())) {
                    switch (dataBaseType) {
                        case TYPE_2:
                            FieldDataType_DM dataType = FieldDataType_DM.getTypeByCode(field.getFieldDataType());
                            List<Map<String, Object>> logicOperatorList = LogicOperator_DM.getLogicOperatorsByDataType(dataType);
                            field.setLogicOperatorList(logicOperatorList);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        return new ResponseDto("获取成功", list);
    }
}